package com.sgcc.lms.openstack.controller;

import com.sgcc.lms.openstack.exception.*;
import com.sgcc.lms.openstack.service.GlanceService;
import com.sgcc.lms.openstack.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.openstack4j.model.common.ActionResponse;
import org.openstack4j.model.image.v2.Image;
import org.openstack4j.openstack.image.v2.domain.GlanceImage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author: 李孟帅
 * @CreateTime: 2020-07-21 14:18
 * @Description: 镜像服务控制层
 */
@RestController
@Slf4j
@Api(tags = "GlanceController：镜像服务")
public class GlanceController {

    @Autowired
    private GlanceService glanceService;
    private static final String LINE = "==========================================》{}";

    /**
     * @Author 李孟帅
     * @Description 列出镜像
     * @Date 2020/7/21
     **/
    @GetMapping("/images")
    @ApiOperation("列出镜像")
    public ResultUtil<?> images(HttpServletRequest request) throws NullTokenException {
        //从请求头获取tokenId
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        List<? extends Image> images = glanceService.images(tokenId);
        log.info(uri+",列出镜像"+LINE,"成功");
        return ResultUtil.success(images);
    }

    /**
     * @Author 李孟帅
     * @Description 获取镜像
     * @Date 2020/7/21
     **/
    @GetMapping("/images/get")
    @ApiOperation("获取镜像")
    public ResultUtil<?> getImage(HttpServletRequest request, @RequestParam("id") String id) throws NullTokenException {
        //从请求头获取tokenId
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        Image image = glanceService.getImage(tokenId, id);
        log.info(uri+",获取镜像"+LINE,"成功");
        return ResultUtil.success(image);
    }

    /**
     * @Author 李孟帅
     * @Description 创建镜像
     * @Date 2020/7/21
     **/
    @PostMapping("/images/create")
    @ApiOperation("创建镜像")
    public ResultUtil<?> createImage(HttpServletRequest request, @RequestBody GlanceImage image) throws NullTokenException {
        //从请求头获取tokenId
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        Image image1 = glanceService.createImage(tokenId, image);
        log.info(uri+",创建镜像"+LINE,"成功");
        return ResultUtil.success(image1);
    }

    /**
     * @Author 李孟帅
     * @Description 更新镜像
     * @Date 2020/7/21
     **/
    @PostMapping("/images/update")
    @ApiOperation("更新镜像")
    public ResultUtil<?> updateImage(HttpServletRequest request, @RequestBody GlanceImage image) throws IOException, HttpException {
        //从请求头获取tokenId
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        Image image1 = glanceService.updateImage(tokenId, image);
        log.info(uri+",更新镜像"+LINE,"成功");
        return ResultUtil.success(image1);
    }

    /**
     * @Author 李孟帅
     * @Description 删除镜像
     * @Date 2020/7/21
     **/
    @GetMapping("/images/delete")
    @ApiOperation("删除镜像")
    public ResultUtil<?> deleteImage(HttpServletRequest request, @RequestParam("id") String id) throws NullTokenException {
        //从请求头获取tokenId
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        ActionResponse delete = glanceService.delete(tokenId, id);
        log.info(uri+",删除镜像"+LINE,"成功");
        return ResultUtil.success(delete);
    }


    /**
     * @Author 李孟帅
     * @Description  上传镜像
     * @Date 2020/7/22
     **/
    @PostMapping("/images/upload")
    @ApiOperation("上传镜像")
    public ResultUtil<?> uploadImage(HttpServletRequest request, @RequestPart("file") MultipartFile file, @RequestParam("id") String id) throws NullTokenException, IOException {
        //从请求头获取tokenId
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        ActionResponse actionResponse = glanceService.uploadImage(tokenId, file, id);
        if (actionResponse.isSuccess()) {
            log.info(uri+",下载镜像"+LINE,"成功");
        }else {
            log.error(uri+",下载镜像"+LINE,"成功");
        }
        return ResultUtil.success(actionResponse);
    }

    /**
    * @Author 李孟帅
    * @Description 下载镜像
    * @Date 2020/7/23
    **/
    @GetMapping("/images/download")
    @ApiIgnore
    public ResultUtil<?> downloadImage(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") String id) throws NullTokenException {
//        //从请求头获取tokenId
//        String tokenId = request.getHeader("tokenId");
//        String uri = request.getRequestURI();
//        ActionResponse actionResponse = glanceService.downloadImage(tokenId, response, id);
//        log.info(uri+",下载镜像"+LINE,"成功");
        return ResultUtil.success();
    }
}
